package com.future.mayi.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.activity.WalletActivity;
import com.future.mayi.adapter.RozenLogAdapter;
import com.future.mayi.adapter.WalletInAdapter;
import com.future.mayi.app.App;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.response.RozenLogResponse;
import com.future.mayi.response.WalletLogResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;

public class RozenLogFragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener {
    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;

    private RozenLogAdapter adapter;

    private int page;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_in_wallet;
    }

    @Override
    protected void initView() {
        recyclerView.setHasFixedSize(true);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new RozenLogAdapter(getContext(), recyclerView);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        recyclerView.setLoadingListener(this);
        adapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        recyclerView.refresh();
    }


    @Override
    public void OnItem(View view, int position) {

    }

    @Override
    public void OnLongItem(View view, int position) {

    }


    @Override
    public void onRefresh() {
        page = 0;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }

    @SuppressWarnings("unchecked")
    private void getData() {
        new HttpRequest<RozenLogResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.rozenLogWallet)
                .addParam("token", App.getToken(mContext))
                .addParam("p", page)
                .addParam("psize", 30)
                .setListener(new HttpRequest.OnNetworkListener<RozenLogResponse>() {

                    @Override
                    public void onSuccess(RozenLogResponse response) {
                        HCUtils.refreshListForPage(recyclerView, adapter, response.list, page, 30);
                    }

                    @Override
                    public void onFail(String message, RozenLogResponse response) {
                        page = HCUtils.refreshFail(recyclerView, page, getContext(), message);
                    }
                }).start(new RozenLogResponse());
    }



    public void refresh() {
        if (recyclerView != null)
            recyclerView.refresh();
    }

}
